package com.lanyou.esb.entity;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;

@Entity
@EntityListeners({ AuditingEntityListener.class })
@Table(name = "T_ESB_BU_BUNDLES")
public class DefinedBundle {

	private String id;
	private String bundleCode;
	private String bundleName;
	private String bundleJarName;
	private String isDefinedBundle;
	private String type;
	private String version;
	private String status;
	private String bundleLocation;
	private Set<Http> https;
	private Set<Ftp> ftps;
	
	/*private String isEnable;*/

	private String creator;

	private String modifier;

	private Date createTime;

	private Date modifyTime;
	
	
	public final static Specification<DefinedBundle> EmptySpecification = new Specification<DefinedBundle>(){
		@Override
		public Predicate toPredicate(Root<DefinedBundle> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<DefinedBundle> idSpecification(String id){
		return new Specification<DefinedBundle>(){
			@Override
			public Predicate toPredicate(Root<DefinedBundle> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "BUNDLE_ID", length = 36)
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name = "BUNDLE_NO", length = 50)
	public String getBundleCode() {
		return bundleCode;
	}
	public void setBundleCode(String bundleCode) {
		this.bundleCode = bundleCode;
	}
	
	@Column(name = "BUNDLE_NAME", length = 50)
	public String getBundleName() {
		return bundleName;
	}
	public void setBundleName(String bundleName) {
		this.bundleName = bundleName;
	}
	
	@Column(name = "BUNDLE_TYPE", length = 2)
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	@Column(name = "VERSION_NO", length = 20)
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	
	@Column(name = "STATUS", length = 2)
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
	
	
	
	
	/*@Column(name = "IS_ENABLE", length = 2)
	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}*/

	@Column(name = "BUNDLE_LOCATION", length = 300)
	public String getBundleLocation() {
		return bundleLocation;
	}



	public void setBundleLocation(String bundleLocation) {
		this.bundleLocation = bundleLocation;
	}



	@ManyToMany
	@JoinTable(name = "T_ESB_BU_IF_BUNDLES_RELATION", 
		joinColumns = @JoinColumn(name = "BUNDLE_ID", referencedColumnName = "BUNDLE_ID") , 
			inverseJoinColumns = @JoinColumn(name = "IF_HTTP_ID", referencedColumnName = "IF_HTTP_ID") 
	)
	public Set<Http> getHttps() {
		return https;
	}



	public void setHttps(Set<Http> https) {
		this.https = https;
	}


	@ManyToMany
	@JoinTable(name = "T_ESB_BU_IF_BUNDLES_RELATION", 
		joinColumns = @JoinColumn(name = "BUNDLE_ID", referencedColumnName = "BUNDLE_ID") , 
			inverseJoinColumns = @JoinColumn(name = "IF_ID", referencedColumnName = "IF_FTP_ID") 
	)
	public Set<Ftp> getFtps() {
		return ftps;
	}



	public void setFtps(Set<Ftp> ftps) {
		this.ftps = ftps;
	}



	@Column(name = "BUNDLE_JAR_NAME", length = 100)
	public String getBundleJarName() {
		return bundleJarName;
	}



	public void setBundleJarName(String bundleJarName) {
		this.bundleJarName = bundleJarName;
	}


	@Column(name = "IS_DEFINED_BUNDLE_JAR", length = 2)
	public String getIsDefinedBundle() {
		return isDefinedBundle;
	}


	public void setIsDefinedBundle(String isDefinedBundle) {
		this.isDefinedBundle = isDefinedBundle;
	}



	@CreatedBy
	@Column(length = 50, updatable = false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable = false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
}
